import 'package:flutter/material.dart';
import 'package:x3_component_core/components/expand_components/base_expand_down.dart';
import 'package:x3_component_core/components/state_components/base_empty_component.dart';

class BaseWrapExpandDown extends StatelessWidget {
  final int limit;
  final List data;
  final Widget Function(dynamic) builder;

  final double _spacing = 8;

  BaseWrapExpandDown({Key key, this.limit = 8, @required this.data, @required this.builder})
      : assert(data != null),
        assert(builder != null),
        super(key: key);
  @override
  Widget build(BuildContext context) {
    if (data.length == 0) return BaseEmptyComponent();
    if (data.length > limit) {
      return BaseExpandDown(
        header: Container(
          margin: EdgeInsets.only(bottom: _spacing),
          child: _buildWrap(data.take(limit).map((e) => builder(e)).toList()),
        ),
        expanded: _buildWrap(data.skip(limit).map((e) => builder(e)).toList()),
      );
    }
    return _buildWrap(data.map((e) => builder(e)).toList());
  }

  Widget _buildWrap(List<Widget> list) {
    return Container(
      alignment: Alignment.centerLeft,
      child: Wrap(
        spacing: _spacing,
        runSpacing: _spacing,
        children: list,
      ),
    );
  }
}
